package cn.jinjimi.app;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;

import java.util.HashMap;

import cn.jinjimi.app.webview.bean.ShareAction;
import cn.jinjimi.base.util.StringUtils;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.OnekeyShareTheme;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;


/**
 * @author zhenhua
 */
public class ShareProxy {

    class ShareState {
        public static final int FAIL = 0;
        public static final int SUCCESS = 1;
        public static final int START = 2;
        public static final int CANCEL = 3;
    }

    public enum ShareType {
        COMMON, IMAGE, MUSIC
    }

    private Activity mContext;
    private String imgUrl;
    private String shareContent;
    private String shareTitle;
    private String from;

    public ShareProxy(Activity ctx) {
        mContext = ctx;
    }


    public void share() {
        parseShareContent();
        doShare2media();
    }

    public static void share(final Context context, final ShareAction action) {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，微信、QQ和QQ空间等平台使用
        oks.setTitle(action.title);
        // titleUrl QQ和QQ空间跳转链接
        oks.setTitleUrl(action.url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(action.content);
        // 分享图片
        oks.setImageUrl(action.previewImageUrl);
        // url在微信、微博，Facebook等平台中使用
        oks.setUrl(action.url);
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
            @Override
            public void onShare(Platform platform, Platform.ShareParams shareParams) {
                if (Wechat.NAME.equals(platform.getName()) || WechatMoments.NAME.equals(platform.getName())) {
                    shareParams.setTitle(action.title);
                    shareParams.setText(action.content);
                    shareParams.setShareType(Platform.SHARE_WEBPAGE);
                    shareParams.setImageData(((BitmapDrawable) ContextCompat.getDrawable(context, R.drawable.logo)).getBitmap());
                    shareParams.setImageUrl(action.previewImageUrl);
                    shareParams.setUrl(action.url);
                }
            }
        });
        // 启动分享GUI
        oks.show(context);
    }


    public void doShare2media() {

    }

    /**
     * 设置分享内容至友盟分享控件
     */
    private void parseShareContent() {

        loadImage(null, imgUrl, true, true);

        //检查分享标题 默认使用app name
        if (StringUtils.isEmpty(shareTitle)) {
            this.shareTitle = mContext.getResources().getString(R.string.app_name);
        }


        //检查分享内容 （shareContent为空时直接分享图片）
        if (TextUtils.isEmpty(shareContent)) {
            this.shareContent = shareTitle;
        }
    }

    public String getFrom() {
        return from;
    }

    public void init(String musicUrl,
                     String imgUrl,
                     String shareUrl,
                     String shareContent,
                     String shareTitle,
                     String from,
                     String notifyUrl,
                     Bitmap bm,
                     ShareType type) {
        this.imgUrl = imgUrl;
        this.shareContent = shareContent;
        this.shareTitle = shareTitle;
        loadImage(bm, imgUrl, false, true);
        this.from = StringUtils.isEmpty(from) ? "ShareDialog" : from;
    }

    private void loadImage(Bitmap bm, final String imageUrl, boolean useDefault, boolean downloadLocal) {

    }

}